package Package.Threads.Question;

public class SellTicket implements Runnable {
    private int tickets = 100;
    private final Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (tickets > 0) {

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售 第" + tickets + "张票");

                    tickets--;
                }
            }
        }
    }
}